package com.jiang.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jiang.factory.ServiceFactory;
import com.jiang.service.IMemberService;
import com.jiang.service.impl.MemberServiceImpl;
import com.jiang.vo.Member;

@WebServlet(name = "MemberServlet", urlPatterns = "/MemberServlet/*")
public class MemberServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8335352428351097482L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 这里主要是靠路径来区分调用的业务方法
		String uri = request.getRequestURI();
		String methodName = uri.substring(uri.lastIndexOf("/") + 1);
		try {
			Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
					HttpServletResponse.class);
			// 直接调用其方法，返回其结果
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * @检查用户名
	 * @param request
	 * @param response
	 */
	public void check(HttpServletRequest request, HttpServletResponse response) {

		String userName = request.getParameter("userName");
		IMemberService memberService = ServiceFactory.getInstance(MemberServiceImpl.class);
		boolean isExist = memberService.isExist(userName);
		try {
			response.getWriter().println(isExist);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response){
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		IMemberService memberService = ServiceFactory.getInstance(MemberServiceImpl.class);
		try{
			Member member = new Member();
			member.setUserName(userName);
			member.setPassWord(passWord);
			memberService.add(member);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
